Midterm 2 - MNIST Classification

Preparing environment and dataset:

The error of your classifier on test dataset must be better then 12.0% LeCun et al. 1998

Enter your error at https://goo.gl/forms/JRDKcotcXf5LZM3I3

Commit your code to github/bitbucket into folder midterm


In [1]:
from mnist import MNIST
import random
mndata = MNIST('.\\data')

trimages, trlabels = mndata.load_training()
teimages, telabels = mndata.load_testing()
index = random.randrange(0, len(trimages))  # choose an index ;-)
print('The amount of train images',len(trimages))
print('The amount of test images',len(trimages))
print('The label of random image',trlabels[index],'The random image is',mndata.display(trimages[index]))
print('Images are binary with 28*28 = ',len(trimages[index]))


The amount of train images 60000
The amount of test images 60000
The label of random image 7 The random image is 
............................
............................
............................
............................
............................
............................
............................
............................
...@................@@......
...@@...........@@@@@@......
...@@@@@@@@@@@@@@@@@@@......
......@@@@@........@@.......
...................@@.......
...................@@.......
..................@@........
..................@@........
.................@@.........
.................@@.........
................@@..........
................@@..........
................@...........
...............@@...........
..............@@............
..............@@............
..............@@............
.............@@@............
.............@@@............
.............@@.............
Images are binary with 28*28 =  784

In [2]:
from keras.models import Sequential
from keras.layers import Dense
import numpy as np
from keras.utils.np_utils import to_categorical

y = to_categorical(trlabels)
X = np.array(trimages)
model = Sequential()
model.add(Dense(128, input_dim=784, init='uniform', activation='relu'))
model.add(Dense(64, init='uniform', activation='relu'))
model.add(Dense(y.shape[1], init='uniform', activation='sigmoid'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])


Using TensorFlow backend.
C:\Users\Gaziz\Anaconda3\lib\site-packages\ipykernel_launcher.py:9: UserWarning: Update your `Dense` call to the Keras 2 API: `Dense(128, input_dim=784, activation="relu", kernel_initializer="uniform")`
  if __name__ == '__main__':
C:\Users\Gaziz\Anaconda3\lib\site-packages\ipykernel_launcher.py:10: UserWarning: Update your `Dense` call to the Keras 2 API: `Dense(64, activation="relu", kernel_initializer="uniform")`
  # Remove the CWD from sys.path while we load stuff.
C:\Users\Gaziz\Anaconda3\lib\site-packages\ipykernel_launcher.py:11: UserWarning: Update your `Dense` call to the Keras 2 API: `Dense(10, activation="sigmoid", kernel_initializer="uniform")`
  # This is added back by InteractiveShellApp.init_path()

In [ ]:


In [3]:
# Fit the model
model.fit(X, y, epochs=140, batch_size=2000, verbose=1)


Epoch 1/140
60000/60000 [==============================] - 1s 22us/step - loss: 0.9754 - acc: 0.6966
Epoch 2/140
60000/60000 [==============================] - 1s 18us/step - loss: 0.2256 - acc: 0.9334
Epoch 3/140
60000/60000 [==============================] - 1s 18us/step - loss: 0.1504 - acc: 0.9555
Epoch 4/140
60000/60000 [==============================] - 1s 18us/step - loss: 0.1154 - acc: 0.9653
Epoch 5/140
60000/60000 [==============================] - 1s 19us/step - loss: 0.0909 - acc: 0.9731
Epoch 6/140
60000/60000 [==============================] - 1s 18us/step - loss: 0.0773 - acc: 0.9764
Epoch 7/140
60000/60000 [==============================] - 1s 19us/step - loss: 0.0620 - acc: 0.9811
Epoch 8/140
60000/60000 [==============================] - 1s 18us/step - loss: 0.0487 - acc: 0.9860
Epoch 9/140
60000/60000 [==============================] - 1s 18us/step - loss: 0.0412 - acc: 0.9884
Epoch 10/140
60000/60000 [==============================] - 1s 18us/step - loss: 0.0349 - acc: 0.9899
Epoch 11/140
60000/60000 [==============================] - 1s 18us/step - loss: 0.0279 - acc: 0.9924
Epoch 12/140
60000/60000 [==============================] - 1s 19us/step - loss: 0.0235 - acc: 0.9936
Epoch 13/140
60000/60000 [==============================] - 1s 19us/step - loss: 0.0201 - acc: 0.9949
Epoch 14/140
60000/60000 [==============================] - 1s 19us/step - loss: 0.0154 - acc: 0.9964
Epoch 15/140
60000/60000 [==============================] - 1s 19us/step - loss: 0.0126 - acc: 0.9973
Epoch 16/140
60000/60000 [==============================] - 1s 19us/step - loss: 0.0110 - acc: 0.9976
Epoch 17/140
60000/60000 [==============================] - 1s 19us/step - loss: 0.0091 - acc: 0.9985
Epoch 18/140
60000/60000 [==============================] - 1s 19us/step - loss: 0.0076 - acc: 0.9987
Epoch 19/140
60000/60000 [==============================] - 1s 18us/step - loss: 0.0059 - acc: 0.9993
Epoch 20/140
60000/60000 [==============================] - 1s 18us/step - loss: 0.0048 - acc: 0.9994
Epoch 21/140
60000/60000 [==============================] - 1s 18us/step - loss: 0.0040 - acc: 0.9995
Epoch 22/140
60000/60000 [==============================] - 1s 18us/step - loss: 0.0033 - acc: 0.9997
Epoch 23/140
60000/60000 [==============================] - 1s 18us/step - loss: 0.0023 - acc: 0.9999
Epoch 24/140
60000/60000 [==============================] - 1s 19us/step - loss: 0.0020 - acc: 0.9999
Epoch 25/140
60000/60000 [==============================] - 1s 19us/step - loss: 0.0016 - acc: 1.0000
Epoch 26/140
60000/60000 [==============================] - 1s 18us/step - loss: 0.0013 - acc: 1.0000
Epoch 27/140
60000/60000 [==============================] - 1s 18us/step - loss: 0.0011 - acc: 1.0000
Epoch 28/140
60000/60000 [==============================] - 1s 18us/step - loss: 9.2505e-04 - acc: 1.0000
Epoch 29/140
60000/60000 [==============================] - 1s 18us/step - loss: 8.2847e-04 - acc: 1.0000
Epoch 30/140
60000/60000 [==============================] - 1s 20us/step - loss: 7.5630e-04 - acc: 1.0000
Epoch 31/140
60000/60000 [==============================] - 1s 20us/step - loss: 6.8635e-04 - acc: 1.0000
Epoch 32/140
60000/60000 [==============================] - 1s 19us/step - loss: 6.3931e-04 - acc: 1.0000
Epoch 33/140
60000/60000 [==============================] - 1s 21us/step - loss: 5.9609e-04 - acc: 1.0000
Epoch 34/140
60000/60000 [==============================] - 1s 19us/step - loss: 5.4068e-04 - acc: 1.0000
Epoch 35/140
60000/60000 [==============================] - 1s 19us/step - loss: 5.0062e-04 - acc: 1.0000
Epoch 36/140
60000/60000 [==============================] - 1s 19us/step - loss: 4.6833e-04 - acc: 1.0000
Epoch 37/140
60000/60000 [==============================] - 1s 20us/step - loss: 4.4701e-04 - acc: 1.0000
Epoch 38/140
60000/60000 [==============================] - 1s 24us/step - loss: 4.1011e-04 - acc: 1.0000
Epoch 39/140
60000/60000 [==============================] - 1s 21us/step - loss: 3.8694e-04 - acc: 1.0000
Epoch 40/140
60000/60000 [==============================] - 1s 19us/step - loss: 3.6120e-04 - acc: 1.0000
Epoch 41/140
60000/60000 [==============================] - 1s 18us/step - loss: 3.4284e-04 - acc: 1.0000
Epoch 42/140
60000/60000 [==============================] - 1s 18us/step - loss: 3.2809e-04 - acc: 1.0000
Epoch 43/140
60000/60000 [==============================] - 1s 18us/step - loss: 3.0704e-04 - acc: 1.0000
Epoch 44/140
60000/60000 [==============================] - 1s 18us/step - loss: 2.8839e-04 - acc: 1.0000
Epoch 45/140
60000/60000 [==============================] - 1s 18us/step - loss: 2.6978e-04 - acc: 1.0000
Epoch 46/140
60000/60000 [==============================] - 1s 18us/step - loss: 2.5917e-04 - acc: 1.0000
Epoch 47/140
60000/60000 [==============================] - 1s 18us/step - loss: 2.4694e-04 - acc: 1.0000
Epoch 48/140
60000/60000 [==============================] - 1s 18us/step - loss: 2.3461e-04 - acc: 1.0000
Epoch 49/140
60000/60000 [==============================] - 1s 18us/step - loss: 2.2112e-04 - acc: 1.0000
Epoch 50/140
60000/60000 [==============================] - 1s 18us/step - loss: 2.1282e-04 - acc: 1.0000
Epoch 51/140
60000/60000 [==============================] - 1s 18us/step - loss: 2.0253e-04 - acc: 1.0000
Epoch 52/140
60000/60000 [==============================] - 1s 18us/step - loss: 1.9530e-04 - acc: 1.0000
Epoch 53/140
60000/60000 [==============================] - 1s 18us/step - loss: 1.8435e-04 - acc: 1.0000
Epoch 54/140
60000/60000 [==============================] - 1s 19us/step - loss: 1.7458e-04 - acc: 1.0000
Epoch 55/140
60000/60000 [==============================] - 1s 18us/step - loss: 1.6955e-04 - acc: 1.0000
Epoch 56/140
60000/60000 [==============================] - 1s 18us/step - loss: 1.6182e-04 - acc: 1.0000
Epoch 57/140
60000/60000 [==============================] - 1s 18us/step - loss: 1.5528e-04 - acc: 1.0000
Epoch 58/140
60000/60000 [==============================] - 1s 18us/step - loss: 1.4758e-04 - acc: 1.0000
Epoch 59/140
60000/60000 [==============================] - 1s 18us/step - loss: 1.4195e-04 - acc: 1.0000
Epoch 60/140
60000/60000 [==============================] - 1s 18us/step - loss: 1.3591e-04 - acc: 1.0000
Epoch 61/140
60000/60000 [==============================] - 1s 18us/step - loss: 1.3179e-04 - acc: 1.0000
Epoch 62/140
60000/60000 [==============================] - 1s 18us/step - loss: 1.2564e-04 - acc: 1.0000
Epoch 63/140
60000/60000 [==============================] - 1s 18us/step - loss: 1.2149e-04 - acc: 1.0000
Epoch 64/140
60000/60000 [==============================] - 1s 18us/step - loss: 1.1833e-04 - acc: 1.0000
Epoch 65/140
60000/60000 [==============================] - 1s 18us/step - loss: 1.1213e-04 - acc: 1.0000
Epoch 66/140
60000/60000 [==============================] - 1s 18us/step - loss: 1.0861e-04 - acc: 1.0000
Epoch 67/140
60000/60000 [==============================] - 1s 18us/step - loss: 1.0369e-04 - acc: 1.0000
Epoch 68/140
60000/60000 [==============================] - 1s 18us/step - loss: 1.0083e-04 - acc: 1.0000
Epoch 69/140
60000/60000 [==============================] - 1s 18us/step - loss: 9.7116e-05 - acc: 1.0000
Epoch 70/140
60000/60000 [==============================] - 1s 18us/step - loss: 9.3645e-05 - acc: 1.0000
Epoch 71/140
60000/60000 [==============================] - 1s 18us/step - loss: 9.0956e-05 - acc: 1.0000
Epoch 72/140
60000/60000 [==============================] - 1s 18us/step - loss: 8.8021e-05 - acc: 1.0000
Epoch 73/140
60000/60000 [==============================] - 1s 18us/step - loss: 8.5180e-05 - acc: 1.0000
Epoch 74/140
60000/60000 [==============================] - 1s 19us/step - loss: 8.2425e-05 - acc: 1.0000
Epoch 75/140
60000/60000 [==============================] - 1s 18us/step - loss: 7.9241e-05 - acc: 1.0000
Epoch 76/140
60000/60000 [==============================] - 1s 19us/step - loss: 7.7038e-05 - acc: 1.0000
Epoch 77/140
60000/60000 [==============================] - 1s 20us/step - loss: 7.4569e-05 - acc: 1.0000
Epoch 78/140
60000/60000 [==============================] - 1s 19us/step - loss: 7.2152e-05 - acc: 1.0000
Epoch 79/140
60000/60000 [==============================] - 1s 19us/step - loss: 7.0185e-05 - acc: 1.0000
Epoch 80/140
60000/60000 [==============================] - 1s 20us/step - loss: 6.7829e-05 - acc: 1.0000
Epoch 81/140
60000/60000 [==============================] - 1s 20us/step - loss: 6.6601e-05 - acc: 1.0000
Epoch 82/140
60000/60000 [==============================] - 1s 19us/step - loss: 6.3327e-05 - acc: 1.0000
Epoch 83/140
60000/60000 [==============================] - 1s 18us/step - loss: 6.1819e-05 - acc: 1.0000
Epoch 84/140
60000/60000 [==============================] - 1s 18us/step - loss: 6.0455e-05 - acc: 1.0000
Epoch 85/140
60000/60000 [==============================] - 1s 18us/step - loss: 5.7978e-05 - acc: 1.0000
Epoch 86/140
60000/60000 [==============================] - 1s 19us/step - loss: 5.6292e-05 - acc: 1.0000
Epoch 87/140
60000/60000 [==============================] - 1s 19us/step - loss: 5.4439e-05 - acc: 1.0000
Epoch 88/140
60000/60000 [==============================] - 1s 20us/step - loss: 5.3104e-05 - acc: 1.0000
Epoch 89/140
60000/60000 [==============================] - 1s 19us/step - loss: 5.1340e-05 - acc: 1.0000
Epoch 90/140
60000/60000 [==============================] - 1s 22us/step - loss: 4.9907e-05 - acc: 1.0000
Epoch 91/140
60000/60000 [==============================] - 1s 19us/step - loss: 4.9067e-05 - acc: 1.0000
Epoch 92/140
60000/60000 [==============================] - 1s 19us/step - loss: 4.7198e-05 - acc: 1.0000
Epoch 93/140
60000/60000 [==============================] - 1s 19us/step - loss: 4.6371e-05 - acc: 1.0000
Epoch 94/140
60000/60000 [==============================] - 1s 20us/step - loss: 4.4603e-05 - acc: 1.0000
Epoch 95/140
60000/60000 [==============================] - 1s 20us/step - loss: 4.3348e-05 - acc: 1.0000
Epoch 96/140
60000/60000 [==============================] - 1s 19us/step - loss: 4.2016e-05 - acc: 1.0000: 0s - loss: 4.0992e-05 - acc: 1
Epoch 97/140
60000/60000 [==============================] - 1s 21us/step - loss: 4.1234e-05 - acc: 1.0000
Epoch 98/140
60000/60000 [==============================] - 1s 22us/step - loss: 4.0034e-05 - acc: 1.0000
Epoch 99/140
60000/60000 [==============================] - 1s 22us/step - loss: 3.8982e-05 - acc: 1.0000
Epoch 100/140
60000/60000 [==============================] - 1s 22us/step - loss: 3.7838e-05 - acc: 1.0000
Epoch 101/140
60000/60000 [==============================] - 1s 20us/step - loss: 3.6858e-05 - acc: 1.0000
Epoch 102/140
60000/60000 [==============================] - 1s 21us/step - loss: 3.6118e-05 - acc: 1.0000
Epoch 103/140
60000/60000 [==============================] - 1s 21us/step - loss: 3.4898e-05 - acc: 1.0000: 0s - loss: 3.4907e-05 - acc: 1.000
Epoch 104/140
60000/60000 [==============================] - 1s 21us/step - loss: 3.4034e-05 - acc: 1.0000
Epoch 105/140
60000/60000 [==============================] - 1s 21us/step - loss: 3.3388e-05 - acc: 1.0000
Epoch 106/140
60000/60000 [==============================] - 1s 20us/step - loss: 3.2500e-05 - acc: 1.0000
Epoch 107/140
60000/60000 [==============================] - 1s 20us/step - loss: 3.1472e-05 - acc: 1.0000
Epoch 108/140
60000/60000 [==============================] - 1s 19us/step - loss: 3.0704e-05 - acc: 1.0000
Epoch 109/140
60000/60000 [==============================] - 1s 23us/step - loss: 2.9947e-05 - acc: 1.0000
Epoch 110/140
60000/60000 [==============================] - 1s 20us/step - loss: 2.9293e-05 - acc: 1.0000
Epoch 111/140
60000/60000 [==============================] - 1s 21us/step - loss: 2.8380e-05 - acc: 1.0000
Epoch 112/140
60000/60000 [==============================] - 1s 20us/step - loss: 2.7740e-05 - acc: 1.0000: 0s - loss: 2.6621e-05 - ac
Epoch 113/140
60000/60000 [==============================] - 1s 21us/step - loss: 2.7161e-05 - acc: 1.0000
Epoch 114/140
60000/60000 [==============================] - 1s 21us/step - loss: 2.6462e-05 - acc: 1.0000
Epoch 115/140
60000/60000 [==============================] - 1s 21us/step - loss: 2.5967e-05 - acc: 1.0000
Epoch 116/140
60000/60000 [==============================] - 1s 23us/step - loss: 2.5209e-05 - acc: 1.0000
Epoch 117/140
60000/60000 [==============================] - 1s 22us/step - loss: 2.4637e-05 - acc: 1.0000
Epoch 118/140
60000/60000 [==============================] - 1s 19us/step - loss: 2.4059e-05 - acc: 1.0000
Epoch 119/140
60000/60000 [==============================] - 1s 21us/step - loss: 2.3486e-05 - acc: 1.0000
Epoch 120/140
60000/60000 [==============================] - 1s 22us/step - loss: 2.2950e-05 - acc: 1.0000
Epoch 121/140
60000/60000 [==============================] - 1s 22us/step - loss: 2.2511e-05 - acc: 1.0000
Epoch 122/140
60000/60000 [==============================] - 1s 22us/step - loss: 2.1850e-05 - acc: 1.0000
Epoch 123/140
60000/60000 [==============================] - 1s 21us/step - loss: 2.1227e-05 - acc: 1.0000
Epoch 124/140
60000/60000 [==============================] - 1s 21us/step - loss: 2.0836e-05 - acc: 1.0000
Epoch 125/140
60000/60000 [==============================] - 1s 19us/step - loss: 2.0341e-05 - acc: 1.0000
Epoch 126/140
60000/60000 [==============================] - 1s 21us/step - loss: 1.9830e-05 - acc: 1.0000
Epoch 127/140
60000/60000 [==============================] - 1s 22us/step - loss: 1.9657e-05 - acc: 1.0000
Epoch 128/140
60000/60000 [==============================] - 1s 23us/step - loss: 1.8991e-05 - acc: 1.0000
Epoch 129/140
60000/60000 [==============================] - 1s 23us/step - loss: 1.8540e-05 - acc: 1.0000
Epoch 130/140
60000/60000 [==============================] - 1s 24us/step - loss: 1.8163e-05 - acc: 1.0000
Epoch 131/140
60000/60000 [==============================] - 1s 22us/step - loss: 1.7775e-05 - acc: 1.0000
Epoch 132/140
60000/60000 [==============================] - 1s 23us/step - loss: 1.7360e-05 - acc: 1.0000
Epoch 133/140
60000/60000 [==============================] - 1s 22us/step - loss: 1.6963e-05 - acc: 1.0000
Epoch 134/140
60000/60000 [==============================] - 1s 22us/step - loss: 1.6629e-05 - acc: 1.0000
Epoch 135/140
60000/60000 [==============================] - 1s 22us/step - loss: 1.6171e-05 - acc: 1.0000
Epoch 136/140
60000/60000 [==============================] - 1s 22us/step - loss: 1.5854e-05 - acc: 1.0000
Epoch 137/140
60000/60000 [==============================] - 1s 22us/step - loss: 1.5527e-05 - acc: 1.0000
Epoch 138/140
60000/60000 [==============================] - 1s 22us/step - loss: 1.5165e-05 - acc: 1.0000
Epoch 139/140
60000/60000 [==============================] - 1s 22us/step - loss: 1.4902e-05 - acc: 1.0000
Epoch 140/140
60000/60000 [==============================] - 1s 21us/step - loss: 1.4506e-05 - acc: 1.0000
Out[3]:
<keras.callbacks.History at 0x18e9387ce48>

In [4]:
from sklearn.metrics import accuracy_score

teX = np.array(teimages)

accuracy_score(model.predict_classes(teX), np.array(telabels))


Out[4]:
0.97819999999999996

In [5]:
accuracy_score(model.predict_classes(X), np.array(trlabels))


Out[5]:
1.0

In [ ]: